#ifndef _CBSOCKET_DEFAULTCALLBACK_H
#define _CBSOCKET_DEFAULTCALLBACK_H

#include <stddef.h>

#include <memory>
#include <list>
#include <vector>

#include "AbstractCallback.h"

#include "Parser.h"

namespace cbsocket
{
    class DefaultCallback : public AbstractCallback
    {
    public:
        DefaultCallback(int fd, Parser *parser);
        
        virtual void dataReady();
        virtual void writeReady();
        virtual void error();
        virtual void hangUp();
        
        virtual bool needCleanUp();
        
        bool write(void const* data, size_t size);
        
    private:
        size_t tryWrite(void const* data, size_t size);
        void pushBuffer(void const* data, size_t size);
        
        std::vector<char> readBuffer;
        std::list<std::vector<char> > buffers;
        std::auto_ptr<Parser> parser;
        size_t pos;
        bool writable, failed, eof;
    };
}

#endif
